import VueRouter from "vue-router";
import Vue from "vue";
Vue.use(VueRouter);

// import Home from "./views/HomePage";//首页
// import ClassificationPage from "./views/ClassificationPage";//分类页
// import CartPage from "./views/CartPage";//购物车页
// import AboutPage from "./views/AboutPage";//关于我页面
// import DetailPage from "./views/DetailPage";//物品详情页面
// import SearchPage from "./views/SearchPage";//搜索物品详情页面
// import LoginPage from "./views/LoginPage";//登录页面
// import SearchComponentPage from "./views/SearchComponentPage";//点击进入搜索页面

const config={
    mode:"hash",
    routes:[
        {
            path:"/",
            redirect:"/home"
        },
        {
            path:"/home",
            name:"home",
            component:()=>import("./views/HomePage"),
            meta:{
                navBarName:"SearchBar",//不区分大小写(貌似)
                bottomBarName:"BottomBar"
            }
        },
        {
            path:"/classify",
            name:"classify",
            component:()=>import("./views/ClassificationPage"),
            meta:{
                navBarName:"SearchBar",
                bottomBarName:"BottomBar"
            }
        },
        {
            path:"/cart",
            name:"cast",
            component:()=>import("./views/CartPage"),
            meta:{
                navBarName:"CartBar",
                bottomBarName:"CartBottomBar",
                private:true
            }
        },
        {
            path:"/about",
            name:"about",
            component:()=>import("./views/AboutPage"),
            meta:{
                bottomBarName:"BottomBar"
            }
        },
        {
            path:"/detail",
            name:"about",
            component:()=>import("./views/DetailPage"),
            meta:{
                bottomBarName:"DetailBottomBar"
            }
        },
        {
            path:'/searchGood',
            name:"searchGood",
            component:()=>import("./views/SearchPage"),
            meta:{
                navBarName:"SearchBarForSearchPage"
            }
        },
        {
            path:"/login",
            name:"login",
            component:()=>import("./views/LoginPage")
        },
        {
            path:"/search",
            name:"search",
            component:()=>import("./views/SearchComponentPage"),
            meta:{
                navBarName:"SearchBarForSearchPage"
            }
        }
    ]
}

const router=new VueRouter(config);

router.beforeEach((to,from,next)=>{
    console.log("全局守卫");
    if(to.meta.private){
        if(localStorage.getItem("token")){
            next();
        }else{
            next("/login");
        };
    }else{
        next();
    }
    // if(to.meta.private&&localStorage.getItem("token")){
    //     next();
    // }else{
    //     next("/login");
    // };
    // next();
});

export default router;
